#include "gcategories.h"
#include "ui_gcategories.h"

GCategories::GCategories(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GCategories)
{
    ui->setupUi(this);

    // On récupère la base
    QSqlDatabase db = QSqlDatabase::database("user");

    //On initialise le model et le tree
    model = new CategoriesModel(db,this);
    ui->treeCategories->setModel(model);
    ui->treeCategories->expandAll();

}

GCategories::~GCategories()
{
    delete ui;
}

void GCategories::setRoot(int r)
{
    root = r;
    model->raffraichir(r);
}


void GCategories::on_treeCategories_clicked(const QModelIndex &index)
{
    ui->groupBox_3->setTitle(index.data().toString());
}

void GCategories::on_buttonControlAjouter_clicked()
{
    if(ui->treeCategories->selectionModel()->hasSelection())
        model->insertRows(0,1,ui->treeCategories->currentIndex());
    else
        model->insertRows(0,1,QModelIndex());

}

void GCategories::on_buttonOrdreMoins_clicked()
{
    model->ordreMoins(ui->treeCategories->currentIndex());
}

void GCategories::on_buttonOrdrePlus_clicked()
{
    model->ordrePlus(ui->treeCategories->currentIndex());
}

void GCategories::on_buttonRaffraichir_clicked()
{
    model->raffraichir(root);
}

void GCategories::on_buttonHierarDescendre_clicked()
{
    model->moveDown(ui->treeCategories->currentIndex());
}

void GCategories::on_buttonHierarMonter_clicked()
{
    model->moveUp(ui->treeCategories->currentIndex());
}
